import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
// import Home from '../components/Home'
// import Welcome from '../components/Welcome'
// import Users from '../components/user/Users'
// import Rights from '../components/power/Rights'
// import Roles from '../components/power/Roles'
// import Cate from '../components/goods/Cate'
// import Params from '../components/goods/Params'
// import Lists from '../components/goods/List'
// import Add from '../components/goods/Add'
// import Order from '../components/order/Order'
// import Report from '../components/report/Report'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
  },
  {
    path: '/home',
    component: () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')
      },
      {
        path: '/users',
        component: () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users')
      },
      {
        path: '/rights',
        component: () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights')
      },
      {
        path: '/roles',
        component: () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles')
      },
      {
        path: '/categories',
        component: () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate')
      },
      {
        path: '/params',
        component: () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params')
      },
      {
        path: '/goods',
        component: () => import(/* webpackChunkName: "List_Add" */ '../components/goods/List')
      },
      {
        path: '/goods/add',
        component: () => import(/* webpackChunkName: "List_Add" */ '../components/goods/Add')
      },
      {
        path: '/orders',
        component: () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order')
      },
      {
        path: '/reports',
        component: () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫 控制访问权限
// 如果用户没有登录，但是直接通过URL访问特定页面，需要重新导航到登录页面
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from从哪个路径跳转而来
  // next是一个函数，表示放行 next()放行 next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
